﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using AssociationManager;
using System.Security.Permissions;

namespace Associations.File
{
    static class Program
    {
        [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Console.WriteLine(FileExtentionInfo(AssocStr.Command, ".doc"), "Command");
            //Console.WriteLine(FileExtentionInfo(AssocStr.DDEApplication, ".doc"), "DDEApplication");
            //Console.WriteLine(FileExtentionInfo(AssocStr.DDEIfExec, ".doc"), "DDEIfExec");
            //Console.WriteLine(FileExtentionInfo(AssocStr.DDETopic, ".doc"), "DDETopic");
            //Console.WriteLine(FileExtentionInfo(AssocStr.Executable, ".doc"), "Executable");
            //Console.WriteLine(FileExtentionInfo(AssocStr.FriendlyAppName, ".doc"), "FriendlyAppName");
            //Console.WriteLine(FileExtentionInfo(AssocStr.FriendlyDocName, ".doc"), "FriendlyDocName");
            //Console.WriteLine(FileExtentionInfo(AssocStr.NoOpen, ".doc"), "NoOpen");
            //Console.WriteLine(FileExtentionInfo(AssocStr.ShellNewValue, ".doc"), "ShellNewValue");

            //	DDEApplication: WinWord
            //DDEIfExec: Ñﻴ߾
            //	DDETopic: System
            //	Executable: C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE
            //	FriendlyAppName: Microsoft Office Word
            //	FriendlyDocName: Microsoft Office Word 97 - 2003 Document
            RegistryPermission f = new RegistryPermission(
   RegistryPermissionAccess.Write,
   @"HKEY_LOCAL_MACHINE\Software\RegisteredApplications");
            f.Demand();
            
            MakeChange();
            Console.ReadLine();
        }
        [RegistryPermission(SecurityAction.Demand)]
        [SecurityPermission(SecurityAction.InheritanceDemand, Assertion = true)]
        public static void MakeChange()
        {
            string iconpath = @"C:\Lab\codeplex\reimage\ReImage\bin\Debug\file.ico";
           // string expath = @"C:\Lab\codeplex\reimage\ReImage\bin\Debug\ReImage.exe";
            string expath = @"C:\Lab\CMD\associationmanager-96133\AssociationManager\CryptPad\bin\Debug\CryptPad.exe";
            using (FileAssociationManager mgr = new FileAssociationManager())
            {

                using (ApplicationAssociation ext = mgr.RegisterFileAssociation("igr"))
                {
                    ext.DefaultIcon = new ApplicationIcon(iconpath);
                    ext.ShellOpenCommand = expath;
                    ext.Associated = true;
                }
            }
            Console.WriteLine("Done");
        }
        [RegistryPermission(SecurityAction.Demand)]
        [SecurityPermission(SecurityAction.InheritanceDemand, Assertion = true)]
        public static void UnMakeChange()
        {
            string iconpath = @"C:\Lab\codeplex\reimage\ReImage\bin\Debug\file.ico";
            string expath = @"C:\Lab\codeplex\reimage\ReImage\bin\Debug\ReImage.exe";
            using (FileAssociationManager mgr = new FileAssociationManager())
            {

                using (ApplicationAssociation ext = mgr.RegisterFileAssociation("txc"))
                {
                    //ext.DefaultIcon = new ApplicationIcon(iconpath);
                    //ext.ShellOpenCommand = expath;
                    //ext.Associated = true;
                    
                }
            }
        }
    

        public static string FileExtentionInfo(AssocStr assocStr, string doctype)
        {
            uint pcchOut = 0;
            AssocQueryString(AssocF.Verify, assocStr, doctype, null, null, ref pcchOut);

            StringBuilder pszOut = new StringBuilder((int)pcchOut);
            AssocQueryString(AssocF.Verify, assocStr, doctype, null, pszOut, ref pcchOut);
            return pszOut.ToString();
        }

        [Flags]
        public enum AssocF
        {
            Init_NoRemapCLSID = 0x1,
            Init_ByExeName = 0x2,
            Open_ByExeName = 0x2,
            Init_DefaultToStar = 0x4,
            Init_DefaultToFolder = 0x8,
            NoUserSettings = 0x10,
            NoTruncate = 0x20,
            Verify = 0x40,
            RemapRunDll = 0x80,
            NoFixUps = 0x100,
            IgnoreBaseClass = 0x200
        }

        public enum AssocStr
        {
            Command = 1,
            Executable,
            FriendlyDocName,
            FriendlyAppName,
            NoOpen,
            ShellNewValue,
            DDECommand,
            DDEIfExec,
            DDEApplication,
            DDETopic
        }

    }
}
